package com.xs.task;

/**
 * Created by FlyDonkey on 2019/4/29 23:02
 */

import com.xs.UserService;
import com.xs.dao.UserDao;
import com.xs.dbs.DataSourceContextHolder;
import com.xs.dbs.DatabaseType;
import com.xs.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class SysSchedule {

    @Autowired
    UserService userService;


    /**
     * 测试多数据源定时任务
     * 异步线程是没有线程绑定的临时变量databaseType
     * 因此这里可以遍历所有的租户数据执行具体业务操作
     */
    @Scheduled(cron = "0/10 * * * * ? ")
    public void queryTask() {
        log.info("-----开始执行定时任务-----");
        System.out.println( "DatabaseType: "+DataSourceContextHolder.getDataSourceKey());
        for (DatabaseType databaseType : DatabaseType.values()) {
            DataSourceContextHolder.setDataSourceKey(databaseType);
            userService.testAsyncQuery();
        }
        log.info("-----结束执行定时任务-----");
    }

}
